<template>
  <div v-loading="true" v-if="loading" class="detail-wapper"/>
  <div v-else class="detail-wapper">
    <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="100px" class="detail-wapper__form">
      <el-form-item label="账号名称" prop="username">
        <el-input v-model="formData.username" placeholder="填写账户名称"/>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="formData.name" placeholder="填写姓名"/>
      </el-form-item>
      <el-form-item label="角色" prop="roleList">
        <el-checkbox-group v-model="formData.roleList">
          <el-checkbox
            v-for="(item, index) in roleOptions"
            :key="index"
            :label="item.roleName"
            :value="item.roleId"/>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getRoleList } from '@/api/authManage'
export default {
  name: 'AccountCreate',
  data() {
    return {
      loading: true,
      formData: {
        username: '',
        name: '',
        roleList: []
      },
      rules: {
        username: [
          { required: true, message: '请填写账户名称', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请填写姓名', trigger: 'blur' }
        ],
        roleList: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ]
      },
      roleOptions: []
    }
  },
  async created() {
    this.loading = true
    var getRoleListPromise = getRoleList({})
    try {
      const { rows } = await getRoleListPromise
      this.roleOptions = rows
    } catch (exp) {
      //
    }
    this.loading = false
  },
  methods: {
    _beforeCheck(formName) {
      return new Promise(resolve => {
        this.$refs[formName].validate((valid) => {
          resolve(valid)
        })
      })
    },
    async submitForm(formName) {
      var check = await this._beforeCheck(formName)
      var {
        username = '',
        name = '',
        roleList = []
      } = this.formData || {}
      var { roleOptions = [] } = this
      var roleIds = roleList.map(roleName => {
        var item = roleOptions.find(item => item.roleName === roleName) || {}
        return item.roleId || ''
      }).filter(i => !!i).join(',')
      if (check) {
        this.$store.dispatch('authManage/addAccount', { userName: username, name, roleIds }).then(() => {
          this.$message({
            type: 'success',
            message: '新建账户成功'
          })
          this.$router.replace({ name: 'AccountManage' })
        }).catch(msg => {
          this.$message({
            type: 'error',
            message: msg
          })
        })
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang="scss" scoped>
.detail-wapper{
    padding:30px;
    &__form{
        width:500px;
        margin:0 auto;
    }
}
</style>
<style lang="scss">
.detail-wapper{
    padding:30px;
    &__form{
        .el-checkbox{
             margin-right:30px;
        }
        .el-checkbox+.el-checkbox{
            margin-left:0;

        }
    }
}
</style>

